package com.example.exception;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 基础异常类
 * 所有业务异常的父类
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class BaseException extends RuntimeException {
    
    /**
     * 错误码
     */
    private String errorCode;
    
    /**
     * 错误消息
     */
    private String errorMessage;
    
    /**
     * 错误详情
     */
    private Object errorDetails;
    
    /**
     * 错误级别
     */
    private ErrorLevel errorLevel;
    
    /**
     * 错误分类
     */
    private ErrorCategory errorCategory;
    
    public BaseException(String errorCode, String errorMessage) {
        super(errorMessage);
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        this.errorLevel = ErrorLevel.ERROR;
        this.errorCategory = ErrorCategory.BUSINESS;
    }
    
    public BaseException(String errorCode, String errorMessage, ErrorLevel errorLevel) {
        super(errorMessage);
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        this.errorLevel = errorLevel;
        this.errorCategory = ErrorCategory.BUSINESS;
    }
    
    public BaseException(String errorCode, String errorMessage, ErrorCategory errorCategory) {
        super(errorMessage);
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        this.errorLevel = ErrorLevel.ERROR;
        this.errorCategory = errorCategory;
    }
    
    public BaseException(String errorCode, String errorMessage, ErrorLevel errorLevel, ErrorCategory errorCategory) {
        super(errorMessage);
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        this.errorLevel = errorLevel;
        this.errorCategory = errorCategory;
    }
    
    public BaseException(String errorCode, String errorMessage, Object errorDetails) {
        super(errorMessage);
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        this.errorDetails = errorDetails;
        this.errorLevel = ErrorLevel.ERROR;
        this.errorCategory = ErrorCategory.BUSINESS;
    }
    
    public BaseException(String errorCode, String errorMessage, Throwable cause) {
        super(errorMessage, cause);
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        this.errorLevel = ErrorLevel.ERROR;
        this.errorCategory = ErrorCategory.SYSTEM;
    }
    
    /**
     * 错误级别枚举
     */
    public enum ErrorLevel {
        INFO("信息"),
        WARN("警告"),
        ERROR("错误"),
        FATAL("致命错误");
        
        private final String description;
        
        ErrorLevel(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
    
    /**
     * 错误分类枚举
     */
    public enum ErrorCategory {
        BUSINESS("业务异常"),
        SYSTEM("系统异常"),
        VALIDATION("参数验证异常"),
        AUTHENTICATION("认证异常"),
        AUTHORIZATION("授权异常"),
        NETWORK("网络异常"),
        DATABASE("数据库异常"),
        EXTERNAL("外部服务异常");
        
        private final String description;
        
        ErrorCategory(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
}
